package com.ruoyi.business.service.wecom.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.dto.admin.wecom.WecomGroupMemberPageQueryDTO;
import com.ruoyi.business.domain.koc.TbUserThirdIdentity;
import com.ruoyi.business.domain.vo.wecom.admin.CustomerChatGroupDetailVO;
import com.ruoyi.business.domain.vo.wecom.admin.TbWecomCustomerChatGroupVO;
import com.ruoyi.business.domain.wecom.TbWecomChatGroup;
import com.ruoyi.business.domain.wecom.TbWecomChatGroupMember;
import com.ruoyi.business.mapper.koc.TbUserThirdIdentityMapper;
import com.ruoyi.business.mapper.wecom.TbWecomChatGroupMapper;
import com.ruoyi.business.mapper.wecom.TbWecomChatGroupMemberMapper;
import com.ruoyi.business.service.wecom.ITbWecomChatGroupMemberService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.wecom.WecomChatGroupMemberStatusEnum;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class TbWecomChatGroupMemberServiceImpl extends ServiceImpl<TbWecomChatGroupMemberMapper, TbWecomChatGroupMember> implements ITbWecomChatGroupMemberService {
    @Resource
    private TbUserThirdIdentityMapper tbUserThirdIdentityMapper;


    @Override
    public TableDataInfo<TbWecomChatGroupMember> sysPageQuery(WecomGroupMemberPageQueryDTO pageQueryDTO, PageQuery pageQuery) {
        LambdaQueryWrapper<TbWecomChatGroupMember> qwl = Wrappers.lambdaQuery(TbWecomChatGroupMember.class)
            .eq(TbWecomChatGroupMember::getChatId, pageQueryDTO.getChatId())
            .like(StringUtils.isNotEmpty(pageQueryDTO.getName()), TbWecomChatGroupMember::getName, pageQueryDTO.getName())
            .like(StringUtils.isNotEmpty(pageQueryDTO.getGroupNickname()), TbWecomChatGroupMember::getGroupNickname, pageQueryDTO.getGroupNickname())
            .eq(ObjectUtils.isNotEmpty(pageQueryDTO.getUserType()), TbWecomChatGroupMember::getUserType, pageQueryDTO.getUserType())
            .eq(ObjectUtils.isNotEmpty(pageQueryDTO.getStatus()), TbWecomChatGroupMember::getStatus, pageQueryDTO.getStatus())
            .eq(ObjectUtils.isNotEmpty(pageQueryDTO.getJoinScene()), TbWecomChatGroupMember::getJoinScene, pageQueryDTO.getJoinScene())
            .orderByDesc(TbWecomChatGroupMember::getJoinTime);
        Page<TbWecomChatGroupMember> page = this.page(pageQuery.build(), qwl);
        return TableDataInfo.build(page);
    }

    @Override
    public List<TbWecomCustomerChatGroupVO> memberGroupList(Integer type, String unionId, String externalUserId, Long userId) {
        if (type == 3) {
            if (ObjectUtil.isEmpty(userId)) {
                throw new ServiceException("type=3时系统用户id不许为空");
            }
            TbUserThirdIdentity tbUserThirdIdentity = tbUserThirdIdentityMapper.selectOne(Wrappers.lambdaQuery(TbUserThirdIdentity.class).eq(TbUserThirdIdentity::getUserId, userId));
            if (ObjectUtil.isEmpty(tbUserThirdIdentity)) {
                throw new ServiceException("系统用户unionId 获取有误");
            }
            type = 1;
            unionId = tbUserThirdIdentity.getIdentityUnionId();
        }
        return this.baseMapper.memberGroupList(type, unionId, externalUserId);
    }

    @Override
    public TableDataInfo<CustomerChatGroupDetailVO> sysQueryPageByParam(String externalUserId, PageQuery pageQuery) {
        // 调用 Mapper 进行分页查询
        IPage<CustomerChatGroupDetailVO> resultPage = this.baseMapper
            .selectChatGroupDetailByExternalUserId(pageQuery.build(), externalUserId);
        return TableDataInfo.build(resultPage);
    }

}
